var config = require('../../../config');
/**
 * 画笔层
 */
require('node-easel');
	var PencilLayer = function(layer,pencilData,width,height){
		//常用标识
		this._layer = layer;
		this.pencil = new createjs.Shape();
		this._width = width;
		this._height = height;
		
		this._layer.cache(0,0,this._width,this._height);
		this._layer.addChild(this.pencil);

		/**
		 * 画画
		 * @param {Object} pencilObj 要画的对象
		 * @param {Function} cb 绘制后的回调
		 */
		this.draw = function(pencilObj,cb){
			var graphics = this.pencil.graphics;

			//1.得到context
			this._graphics_arr || ( this._graphics_arr = [] );
			var x;
			for( x in this._graphics_arr ){
				if( this._graphics_arr[x] == pencilObj.stroke_id ){
					graphics = this._graphics_arr[x];
				}
			}
			graphics.strokeId = pencilObj.stroke_id;
			this._graphics_arr.push(graphics);

			switch (pencilData._painterMode){
				case "erase":
					graphics.setStrokeStyle(this.__computeLineWidthFromSpeed(pencilData._painterLineWidth,pencilObj),"round","round");
					graphics.beginStroke("#FFFFFF");//颜色
					if( pencilObj.phase == 0 ){
						this._last_erase_x = pencilObj.x;
						this._last_erase_y = pencilObj.y;
						
					}else if( pencilObj.phase == 1 ){
						if(!this._last_erase_x || !this._last_erase_y){
							this._last_erase_x = pencilObj.x;
							this._last_erase_y = pencilObj.y;
						}
						graphics.moveTo(this._last_erase_x, this._last_erase_y); // 移动到坐标 50 50
						graphics.quadraticCurveTo((pencilObj.x+this._last_erase_x)/2,(pencilObj.y+this._last_erase_y)/2,pencilObj.x,pencilObj.y);
						graphics.endStroke();
						this._last_erase_x = pencilObj.x;
						this._last_erase_y = pencilObj.y;
						this._layer.updateCache("destination-out");
						graphics.clear();
					}else{
						if(!this._last_erase_x || !this._last_erase_y){
							this._last_erase_x = pencilObj.x;
							this._last_erase_y = pencilObj.y;
						}
						
						graphics.moveTo( this._last_erase_x, this._last_erase_y ); // 移动到坐标
						graphics.quadraticCurveTo((pencilObj.x+this._last_erase_x)/2,(pencilObj.y+this._last_erase_y)/2,pencilObj.x,pencilObj.y);
						graphics.endStroke();
						this._last_erase_x = this._last_erase_y = null; 
						this._layer.updateCache("destination-out");
						graphics.clear();
					}
					break;
				case "pencil":
					graphics.setStrokeStyle(this.__computeLineWidthFromSpeed(pencilData._painterLineWidth,pencilObj),"round","round");
					graphics.beginStroke(pencilData._painterColor);//颜色
					if( pencilObj.phase == 0 ){
						this._last_render_x = pencilObj.x;
						this._last_render_y = pencilObj.y;
					}else if( pencilObj.phase == 1 ){
						if(!this._last_render_x || !this._last_render_y){
							this._last_render_x = pencilObj.x;
							this._last_render_y = pencilObj.y;
						}
						graphics.moveTo(this._last_render_x, this._last_render_y); // 移动到坐标 50 50
						graphics.quadraticCurveTo((pencilObj.x+this._last_render_x)/2,(pencilObj.y+this._last_render_y)/2,pencilObj.x,pencilObj.y);
						graphics.endStroke();
						this._last_render_x = pencilObj.x;
						this._last_render_y = pencilObj.y;
						this._layer.updateCache("source-over");
						graphics.clear();
					}else{
						if(!this._last_render_x || !this._last_render_y){
							this._last_render_x = pencilObj.x;
							this._last_render_y = pencilObj.y;
						}
						graphics.moveTo( this._last_render_x, this._last_render_y ); // 移动到坐标
						graphics.quadraticCurveTo((pencilObj.x+this._last_render_x)/2,(pencilObj.y+this._last_render_y)/2,pencilObj.x,pencilObj.y);
						graphics.endStroke();
						this._last_render_x = this._last_render_y = null;
						this._layer.updateCache("source-over");
						graphics.clear();
					}
					break;
				default:
					console.log("未定义的工具！");
					console.log(JSON.stringify(pencilObj));
					break;
			}

			if(pencilObj.phase == 0 || pencilObj.phase == 1){
				cb && cb(pencilObj);
				return;
			}
				

			//3.删除context
			var remaining = [];
			var x;
			for(x in this._graphics_arr ){
				var graphics = this._graphics_arr[x];
				if( graphics.strokeId != pencilObj.stroke_id )
					remaining.push(graphics);
			}

			if (remaining.length)
				this._graphics_arr = remaining;
			else
				this._graphics_arr = null;
			cb && cb(pencilObj);
		}
		
		/**
		 * 清除画笔
		 */
		this.clear = function(){
			var graphics = this.pencil.graphics;
			graphics.beginFill("#FFFFFF").drawRect(0,0,this._width,this._height);
			this._layer.updateCache("destination-out");
			graphics.clear();
		}

		/**
		 * 计算并返回贝塞尔控制点
		 * @private
		 */
		this.__computeBezierPoint = function(){

		}
		
		/**
		 * 画笔宽度优化
		 * @param {int} base_width 基础宽度
		 * @param {Object} obj 要计算的对象
		 */
		this.__computeLineWidthFromSpeed = function(base_width, obj){
			var width = base_width;
			//根据当前屏幕宽高换算出对应ipad的x，y坐标(1024x693对应ipad宽高)
			var myX = obj.x*1024/config.screenWidth;
			var myY = obj.y*693/config.screenHeight;
			var level = 2; //平滑度
			if( obj.phase != 0 ){
				var l = Math.sqrt( ( myX - this._last_x ) * ( myX - this._last_x ) + ( myY - this._last_y ) * ( myY - this._last_y ) );
				var v = ( obj.timestamp - this._last_t ) / l;//v的倒数
				this._base_v = 0.0016;
				var p = v / this._base_v ;
				for( var i = 0; i<level; p = Math.sqrt(p),i++ );

				var tmp_w = p * base_width;

				if(tmp_w==0){tmp_w=base_width;}//2015.6.5=====JackYan

				(width > tmp_w) && ( width = tmp_w );
			}
			if( obj.phase == 2 ){
				this._last_t = this._last_x = this._last_y = this._base_v = void 0;
			}else{
				this._last_t = obj.timestamp;
				this._last_x = myX;
				this._last_y = myY;
			}
			return width;
		}
		
	};

module.exports = PencilLayer;